<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { getOrderListApi, deleteOrderApi } from '@/api/check/order'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref, reactive } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { OrderType } from '@/api/check/order/types'
import { useRouter } from 'vue-router'
import { ElButton } from 'element-plus'

const { push } = useRouter()

const ids = ref<string[] | number[]>([])

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getOrderListApi({
      page: unref(currentPage),
      limit: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data,
      total: res.count
    }
  },
  fetchDelApi: async () => {
    const res = await deleteOrderApi(unref(ids))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods

const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    fixed: true,
    type: 'selection',
    width: 80,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'index',
    label: t('common.index'),
    type: 'index',
    fixed: true,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'order_type',
    label: '试验单类型',
    minWidth: 90
  },
  {
    field: 'order_no',
    label: '试验单号',
    minWidth: 120
  },
  {
    field: 'material_description',
    label: '材料说明',
    minWidth: 400
  },
  {
    field: 'prepared_date',
    label: '委托时间',
    search: {
      hidden: true
    }
  },
  {
    field: 'finished_date',
    label: '试验完成时间',
    search: {
      hidden: true
    }
  },
  {
    field: 'status',
    label: '状态',
    search: {
      hidden: true
    },
    form: {
      hidden: false
    }
  },
  {
    field: 'create_user',
    label: '创建人',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'create_datetime',
    label: '创建时间',
    minWidth: 180,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'action',
    label: t('common.action'),
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <ElButton type="primary" onClick={() => action(data.row.id)}>
                编辑
              </ElButton>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const action = (v: string | undefined) => {
  push(v ? `/check/order-add/${v}` : '/check/order-add')
}

const delLoading = ref(false)

const delData = async (row: OrderType | null) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row ? [row.id] : elTableExpose?.getSelectionRows().map((v: OrderType) => v.id) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <div class="mb-10px">
      <!-- <ElButton type="primary" @click="action">{{ t('common.add') }}</ElButton> -->
      <!-- <ElButton :loading="delLoading" type="danger" @click="delData(null)">
        {{ t('common.del') }}
      </ElButton> -->
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
      :fit="true"
    />
  </ContentWrap>
</template>
